package cn.com.headfree.evaluation.service.impl;

import cn.com.headfree.depterprela.service.OrgDeptService;
import cn.com.headfree.entity.TDeclareEvaluationPoint;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.TRecordOptLog;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.evaluation.dao.IDeclareEvaluationDao;
import cn.com.headfree.evaluation.dao.IEvaluationPointManageDao;
import cn.com.headfree.evaluation.service.IEvaluationPointManageService;
import cn.com.headfree.recordlog.service.IRecordLogService;
import cn.com.headfree.vo.TDeclareEvaluationPointVo;
import com.gta.edu.sdk.common.dto.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2022/6/28
 **/
@Service("evaluationPointManageService")
public class EvaluationPointManageServiceImpl implements IEvaluationPointManageService {

    @Autowired
    private IEvaluationPointManageDao evaluationPointManageDao;
    @Autowired
    private IDeclareEvaluationDao declareEvaluationDao;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private IRecordLogService recordLogService;

    @Override
    public void findDeclareEvaluationPointPageList(Page<TDeclareEvaluationPointVo> page, TDeclareEvaluationPointVo declareEvaluationPoint) {
        evaluationPointManageDao.findDeclareEvaluationPointPageList(page,declareEvaluationPoint);
    }

    @Override
    public List<TDeclareEvaluationPointVo> findDeclareEvaluationPointList(TDeclareEvaluationPointVo declareEvaluationPointVo) {
        return evaluationPointManageDao.findDeclareEvaluationPointList(declareEvaluationPointVo);
    }

    @Override
    public void saveInfo(TDeclareEvaluationPoint declareEvaluationPoint, User user) {
        //如果已经有积分明细，则覆盖
        evaluationPointManageDao.deleteDeclareEvaluationPoint(declareEvaluationPoint.getDeclareId());
        //如果是对审核不通过的申报进行编辑再提交，则将申报数据由不通过修改为待审核
        declareEvaluationDao.updateDeclareEvaluationState(declareEvaluationPoint.getDeclareId(),"0",null);
        declareEvaluationPoint.setErpNo(user.getFax());
        declareEvaluationPoint.setUserName(user.getName());
        declareEvaluationPoint.setCreaterId(user.getFax());
        declareEvaluationPoint.setCreaterName(user.getName());
        declareEvaluationPoint.setCreateTime(new Date());
        declareEvaluationPoint.setUpdaterId(user.getFax());
        declareEvaluationPoint.setUpdaterName(user.getName());
        declareEvaluationPoint.setUpdateTime(new Date());
        declareEvaluationPoint.setOrgId(user.getBaseOrgId());
        declareEvaluationPoint.setDelSign(0);
        //所属部门（一级部门）
        TOrgDept dept = new TOrgDept();
        dept = getDeptId(user.getDeptId(),dept,user);
        declareEvaluationPoint.setDeptId(dept.getId());
        declareEvaluationPoint.setDeptName(dept.getName());
        //获取上年度剩余积分
        evaluationPointManageDao.saveInfo(declareEvaluationPoint);

        //日志
        TRecordOptLog log = new TRecordOptLog();
        log.setCurrentRecordId(declareEvaluationPoint.getDeclareId());
        log.setRecordId(declareEvaluationPoint.getDeclareId());
        log.setOptType("0");
        log.setOptContent(user.getName()+"提报了人才评价申报");
        recordLogService.save(user,log);

    }

    @Override
    public void updateEvaluationPoint(TDeclareEvaluationPoint declareEvaluationPoint, User user) {
        declareEvaluationPoint.setUpdaterId(user.getFax());
        declareEvaluationPoint.setUpdaterName(user.getName());
        declareEvaluationPoint.setUpdateTime(new Date());
        evaluationPointManageDao.updateEvaluationPoint(declareEvaluationPoint);
        TRecordOptLog log = new TRecordOptLog();
        log.setCurrentRecordId(declareEvaluationPoint.getDeclareId());
        log.setRecordId(declareEvaluationPoint.getDeclareId());
        log.setOptType("2");
        log.setOptContent(user.getName()+"调整了得分,原因是"+declareEvaluationPoint.getTrimReason());
        recordLogService.save(user,log);

    }

    @Override
    public TDeclareEvaluationPointVo getDeclareEvaluationPoint(String year, String orgId, String erpNo) {
        return evaluationPointManageDao.getDeclareEvaluationPoint(year,orgId,erpNo);
    }

    @Override
    public TDeclareEvaluationPointVo selectTDeclareEvaluationPointById(String declareId) {
        return evaluationPointManageDao.selectTDeclareEvaluationPointById(declareId);
    }

    //获取当前登录人所在一级部门ID
    public TOrgDept getDeptId(String deptId,TOrgDept orgDept,User user){
        TOrgDept dept = orgDeptService.getOrgDeptById(deptId);
        if(dept != null){
            if(dept.getpId().equals(user.getBaseOrgId())){
                orgDept.setId(dept.getId());
                orgDept.setName(dept.getName());
            }else{
                getDeptId(dept.getpId(),dept,user);
            }
        }
        return dept;
    }
}
